package com.xiaodou.mjf.web.console.merchant;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.xiaodou.mjf.application.core.service.OrderService;
import com.xiaodou.mjf.application.core.service.OrderStatementService;
import com.xiaodou.mjf.entity.core.Order;
import com.xiaodou.mjf.entity.core.OrderStatement;
import com.xiaodou.mjf.infrastructure.common.context.SpringContextHolder;
import com.xiaodou.mjf.infrastructure.common.utils.DateUtils;

/**
 * 商户结算
 * 
 * @author BILLY
 *
 */
@Component
@Transactional
public class MerchantSettlement {

	private static OrderService orderService = SpringContextHolder.getBean(OrderService.class);
	private static OrderStatementService orderStatementService = SpringContextHolder.getBean(OrderStatementService.class);

	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	static Calendar cal = Calendar.getInstance();

	/**
	 * 得到某年某周的第一天
	 *
	 * @param year
	 * @param week
	 * @return
	 */
	public static String getFirstDayOfWeek(int year, int week) {
		cal.setTime(DateUtils.getCurrentTime());
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置周
		cal.set(Calendar.WEEK_OF_YEAR, week);
		// 设置该周第一天为星期一
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		// 格式化日期
		String firstDayOfWeek = sdf.format(cal.getTime());

		return firstDayOfWeek;
	}

	/**
	 * 得到某年某周的最后一天
	 *
	 * @param year
	 * @param week
	 * @return
	 */
	public static String getLastDayOfWeek(int year, int week) {
		cal.setTime(DateUtils.getCurrentTime());
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置周
		cal.set(Calendar.WEEK_OF_YEAR, week);
		// 设置该周第一天为星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 设置最后一天是星期日
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 6); // Sunday
		// 格式化日期
		String lastDayOfWeek = sdf.format(cal.getTime());

		return lastDayOfWeek;
	}

	/**
	 * 商户结算
	 * 
	 * @throws Exception
	 */
	public static void settlement() throws Exception {
		try {
			Integer year = cal.get(Calendar.YEAR);
			Integer week = cal.get(Calendar.WEEK_OF_YEAR);
			String beginDate = getFirstDayOfWeek(year, week);
			String endDate = getLastDayOfWeek(year, week);
			List<OrderStatement> orderStatements = Lists.newArrayList();
			List<Order> orderLists = orderService.getOrderDayOfWeek(sdf.parse(beginDate), sdf.parse(endDate));
			if (orderLists.size() > 0) {
				for (Order order : orderLists) {
					OrderStatement entity = new OrderStatement();
					entity.setMerchantId(order.getMerchantId());
					entity.setYear(year);
					entity.setStage(week);
					entity.setOrderId(order.getId());
					entity.setExtOrderId(order.getExtOrderId());
					entity.setDescription(order.getDescription());
					entity.setAmount(order.getAmount());
					entity.setProcDate(order.getProcDate());
					entity.setStatementStatus(order.getPaymentStatus());
					entity.setStatus(order.getStatus());
					entity.setMerchantName(order.getMerchantName());
					orderStatements.add(entity);
				}
			}
			orderStatementService.save(orderStatements);

			System.out.println("数据保存成功");

		} catch (Exception ex) {

			throw ex;
		}
	}

	public static void main(String[] args) throws Exception {

		settlement();
	}

}
